/*
  年月日联动下拉选择JS封装类 Ver 1.0 版
  制作时间:2013-3-12
　更新时间:2013-3-12
  应用说明:页面包含<script type="text/javascript" src="YMDClass.js" charset="utf-8"></script>
  <select name="year1"></select>
  <select name="month1"></select>
  <select name="day1"></select>
  <script>
  new YMDselect('year1','month1','day1',1990,2,10);
  </script>
	年月联动
	  new YMDselect('year1','month1');
	  new YMDselect('year1','month1',1990);
	  new YMDselect('year1','month1',1990,2);
	年月日联动
	  new YMDselect('year1','month1','day1');
	  new YMDselect('year1','month1','day1',1990);
	  new YMDselect('year1','month1','day1',1990,2);
	  new YMDselect('year1','month1','day1',1990,2,10);
\*** 程序制作/版权所有:Kevin QQ:251378427 E-Mail:yeminch@qq.com 网址:http://iulog.com ***/
export default {
    YMDselect
}
let SYT="年";
let SMT="月";
let SDT="日";
let BYN=50;//年份范围往前50年
let AYN=0;//年份范围往后0年
let dDate = "";
let dCurYear = "";
let daysInMonth = "";
let YI = "";
let MI = "";
let dPrevDate = "";
function YMDselect(){
	this.SelY=document.getElementsByName(arguments[0])[0];
	this.SelM=document.getElementsByName(arguments[1])[0];
	this.SelD=document.getElementsByName(arguments[2])[0];
	//得到年月日对应的dom元素
	this.DefY=this.SelD?arguments[3]:arguments[2];
    console.log(this.DefY)
	this.DefM=this.SelD?arguments[4]:arguments[3];
	this.DefD=this.SelD?arguments[5]:arguments[4];
	console.log(this.DefD);
	this.SelY.YMD=this;
	this.SelM.YMD=this;
	this.SelY.onchange=function(){YMDselect.SetM(this.YMD)};
	if(this.SelD)this.SelM.onchange=function(){YMDselect.SetD(this.YMD)};
	YMDselect.SetY(this);

};
//设置年份
YMDselect.SetY=function(YMD){
	dDate = new Date();
	dCurYear = dDate.getFullYear();
	YMD.SelY.options.add(new Option(SYT,''));
	for(var i = dCurYear+AYN; i>(dCurYear-BYN); i--){
		var YMDYT=i+'年';
		var YMDYV=i;
		var OptY = new Option(YMDYT,YMDYV);
		YMD.SelY.options.add(OptY);
		if(YMD.DefY==YMDYV){
            OptY.selected=true;
        }
	}
	YMDselect.SetM(YMD);
};
//设置月份
YMDselect.SetM=function(YMD){
	var mNum = 12;
	if(YMD.SelY.value== dCurYear){
		mNum = (dDate.getMonth()+1);
	}
	YMD.SelM.length = 0;
	YMD.SelM.options.add(new Option(SMT,''));
	if(YMD.SelY.value>0){
		for(var i=1;i<=mNum;i++){
			var YMDMT=i+'月';
			var YMDMV=i>9?i:'0'+i;
			var OptM=new Option(YMDMT,YMDMV);
			YMD.SelM.options.add(OptM);
			if(YMD.DefM==YMDMV) OptM.selected=true
		}
	}
	if(YMD.DefD){
        if(YMD.SelY.value== dCurYear){
            var OptM=new Option("至今","至今");
            YMD.SelM.options.add(OptM);
        }
    }

	if(YMD.SelD)YMDselect.SetD(YMD)
};
//设置日期
YMDselect.SetD=function(YMD){
	YI=YMD.SelY.value;
	MI=YMD.SelM.value;
	YMD.SelD.length = 0;
	YMD.SelD.options.add(new Option(SDT,''));
	if(YI>0 && MI>0){
		dPrevDate = new Date(YI, MI, 0);

		if(MI==(dDate.getMonth()+1)&&dCurYear==YI){
			daysInMonth = dDate.getDate();
		}else{
			daysInMonth=dPrevDate.getDate();
		}
		for (var d = 1; d <= parseInt(daysInMonth); d++) {
			var YMDDT=d+'日';
			var YMDDV=d>9?d:'0'+d;
			var OptD=new Option(YMDDT,YMDDV);
			YMD.SelD.options.add(OptD);
			if(YMD.DefD==YMDDV)OptD.selected=true
		}
	}
}

